﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class DrawQuad : MonoBehaviour {

    void Start()
    {
        //gameObject.AddComponent<MeshFilter>();
        //gameObject.AddComponent<MeshRenderer>();
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button")) TestUV();

    }

    void TestUV()
    {
        Mesh mesh = new Mesh();

        Vector3[] vertices = new Vector3[4];
        vertices[0] = new Vector3(0, 0, 0); //top-left
        vertices[1] = new Vector3(2, 0, 0); //top-right
        vertices[2] = new Vector3(0, -2, 0); //bottom-left
        vertices[3] = new Vector3(2, -2, 0); //bottom-right

        mesh.vertices = vertices;

        int[] triangles = new int[6] { 0, 1, 2, 3, 2, 1 };
        mesh.triangles = triangles;

        //this is also acceptable!
        //mesh.SetTriangleStrip(new int[4]{0,1,2,3}, 0);

        Vector2[] uvs = new Vector2[4];
        uvs[0] = new Vector2(0, 1); //top-left
        uvs[1] = new Vector2(1, 1); //top-right
        uvs[2] = new Vector2(0, 0); //bottom-left
        uvs[3] = new Vector2(1, 0); //bottom-right

        mesh.uv = uvs;

        Vector3[] normals = new Vector3[4] { Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward };
        mesh.normals = normals;

        //you could also call this instead...
        //mesh.RecalculateNormals();


        //grab our filter.. set the mesh
        MeshFilter filter = GetComponent<MeshFilter>();
        filter.mesh = mesh;

        //you can do your material stuff here...
        //MeshRenderer r = GetComponent<MeshRenderer>();
        ////r.material = new Material(Shader.Find("Diffuse"));
        //r.material = Resources.GetBuiltinResource(typeof(Material), "Default-Diffuse") as Material;
    }
}
